<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="plugins/ztree/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="plugins/ztree/js/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="plugins/ztree/js/jquery.ztree.excheck-3.5.js"></script>
</head>
<body>

<script>
    //1.ztree的设置
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    //2.ztree的数据
    var zNodes =[
        { id:11, pId:1, name:"随意勾选 1-1"},
        { id:111, pId:11, name:"随意勾选 1-1-1"},
        { id:112, pId:11, name:"随意勾选 1-1-2"},
        { id:12, pId:1, name:"随意勾选 1-2"},
        { id:121, pId:12, name:"随意勾选 1-2-1"},
        { id:122, pId:12, name:"随意勾选 1-2-2"},
        { id:2, pId:0, name:"随意勾选 2"},
        { id:21, pId:2, name:"随意勾选 2-1"},
        { id:22, pId:2, name:"随意勾选 2-2"},
        { id:221, pId:22, name:"随意勾选 2-2-1"},
        { id:222, pId:22, name:"随意勾选 2-2-2"},
        { id:23, pId:2, name:"随意勾选 2-3"},
        { id:1, pId:0, name:"随意勾选 1"}
    ];
    //3.页面初始化的时候，加载ztree树
    var zTreeObj;
    $(document).ready(function(){
        //init方法，初始化ztree树
        zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);  //1.dom域，2、ztree的设置，3、数据
        //可以调用zTreeObj对象的方法
        zTreeObj.expandAll(true); //true:全部展开，false：折叠
    });


    /**
     * 调用Ztree的API获取到选中节点的数据
     */
    function getNodesId() {
        //获取所有被勾选节点的集合
        var nodes = zTreeObj.getCheckedNodes(true); //true:被勾选数据，false：未被勾选的
        for(var i=0;i<nodes.length;i++) {
            var node = nodes[i];
            console.log(node.id);
        }
    }
</script>

<div>
    <ul id="treeDemo" class="ztree"></ul>
    <button onclick="getNodesId()">保存</button>
</div>
</body>
</html>
